package com.campsys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.campsys.common.Result;
import com.campsys.entity.CampClass;
import com.campsys.entity.User;
import com.campsys.service.CampClassService;
import com.campsys.service.dto.CampClassDTO;
import com.campsys.service.dto.CampClassQueryDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/class")
@Slf4j
public class AdminClassController {
    
    @Autowired
    private CampClassService classService;
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:view")
    @PostMapping("/list")
    public Result getClassList(@RequestBody CampClassQueryDTO queryDTO) {
        Page<CampClass> page = new Page<>(queryDTO.getPageNum(), queryDTO.getPageSize());
        return Result.ok(classService.getClassList(page, queryDTO));
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:add")
    @PostMapping("/add")
    public Result addClass(@RequestBody @Validated CampClassDTO classDTO) {
        classService.addClass(classDTO);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:edit")
    @PostMapping("/update")
    public Result updateClass(@RequestBody @Validated CampClassDTO classDTO) {
        if (classDTO.getId() == null) {
            return Result.error("班级ID不能为空");
        }
        classService.updateClass(classDTO);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:delete")
    @PostMapping("/delete/{id}")
    public Result deleteClass(@PathVariable Long id) {
        classService.deleteClass(id);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:edit")
    @PostMapping("/status/{id}")
    public Result updateClassStatus(@PathVariable Long id, @RequestParam Integer status) {
        if (status != 0 && status != 1) {
            return Result.error("状态值只能是0或1");
        }
        classService.updateStatus(id, status);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:student")
    @PostMapping("/{id}/students")
    public Result getClassStudents(@PathVariable Long id, @RequestParam Integer pageNum, @RequestParam Integer pageSize) {
        Page<User> page = new Page<>(pageNum, pageSize);
        return Result.ok(classService.getClassStudents(page, id));
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:student")
    @PostMapping("/{id}/students/import")
    public Result importStudents(@PathVariable Long id, @RequestBody List<Long> studentIds) {
        classService.importStudents(id, studentIds);
        return Result.ok();
    }
    
    @RequiresRoles(value = {"admin"})
    @RequiresPermissions("class:student")
    @PostMapping("/{id}/students/{studentId}/remove")
    public Result removeStudent(@PathVariable Long id, @PathVariable Long studentId) {
        classService.removeStudent(id, studentId);
        return Result.ok();
    }
} 